package com.fecean.aabbc.shardingsphere;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * CodeGenerator
 *
 * @author: <a href="mailto:chenfenghai@oristartech.com">chenfenghai</a>
 * @version: 1.0
 * @date: 2019-10-24 17:07
 */
public class CodeGenerator {

    //代码生成器
    private static AutoGenerator mpg = new AutoGenerator();

    //全局配置
    private static  GlobalConfig gc = new GlobalConfig();

    //作者、包名、去除表前缀
    private static final String author = "chenhai";
    private static final String package_name = "com.fecean.aabbc.shardingsphere";
    private static final String TABLE_PREFIX = "";

    //数据库
    private static final String url = "jdbc:mysql://192.168.102.213:3306/CMS_NEW?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true";
    private static final String driverName = "com.mysql.cj.jdbc.Driver";
    private static final String userName = "cms";
    private static final String password = "123456";
    private static final String table_name = "user";


    public static void main(String[] args){
        // 数据源配置
        setDataSource();
        // 全局配置
        setGlobalConfig();
        // 策略配置
        setStrategy();
        //执行
        mpg.execute();
    }

    private static void setStrategy() {
        StrategyConfig strategy = new StrategyConfig();
        // 类名：Tb_userController -> TbUserController
        strategy.setNaming(NamingStrategy.underline_to_camel);
        // 属性名：start_time -> startTime
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        // lombok 代替 setter/getter等方法
        strategy.setEntityLombokModel(true);
        // 设置Controller为RestController
        strategy.setRestControllerStyle(true);
        //由数据库该表生成
        strategy.setInclude(table_name);
        //去除表前缀
        strategy.setTablePrefix(TABLE_PREFIX);
        mpg.setStrategy(strategy);
    }

    private static void setGlobalConfig() {
        URL urlPath = Thread.currentThread().getContextClassLoader().getResource("");
        String projectPath = Objects.requireNonNull(urlPath).getPath().replace("/target/classes", "");
        String javasrc = projectPath + "src/main/java";
        gc.setOutputDir(javasrc);//代码生成位置
        gc.setFileOverride(true);//覆盖已有文件
        gc.setAuthor(author);
        gc.setSwagger2(false);
        gc.setIdType(IdType.INPUT);//主键ID类型
        gc.setDateType(DateType.ONLY_DATE);//设置时间类型为Date
//        gc.setEntityName(entityName);
//        gc.setMapperName(name_prefix + "Mapper");
//        gc.setControllerName(name_prefix + "Controller");
//        gc.setServiceName(name_prefix + "Service");
//        gc.setServiceImplName(name_prefix + "ServiceImpl");
        mpg.setGlobalConfig(gc);
        PackageConfig pc = new PackageConfig();// 包配置
        pc.setParent(package_name);

        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // 自定义配置会被优先输出
        focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名 ， 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化！！
                return projectPath + "src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
            }
        };
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);
        mpg.setPackageInfo(pc);
    }

    private static void setDataSource() {
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(url);
        dsc.setDriverName(driverName);
        dsc.setUsername(userName);
        dsc.setPassword(password);
        mpg.setDataSource(dsc);
    }
}
